$(function () {
    $('#add_btn').click(function () {
        methods.addHandle()
    })
    $('#exampleTableEvents').on('click', '#TableEditor', function () {
        trIndex = $('#TableEditor', '#exampleTableEvents').index($(this));
        addEnter = false;
        $(this).parents('tr').addClass('has_case');
        methods.editHandle(trIndex);
    })
    // $('#search_btn').click(function () {
    //     methods.seachName();
    // })
    // $('#back_btn').click(function () {
    //     $('#khmc').val(' ');
    //     methods.resectList();
    // })
    $('#TableDelete').click(function () {
        $(this).parents('tr').remove();
    })
    $('.xzdd').on('hide.bs.modal', function () {
        addEnter = true;
        $('#exampleTableEvents tr').removeClass('has_case');
        $('#exampleTableEvents input').val(' ');
        $('#exampleTableEvents select').find('option:first').prop('selected', true)
    });
})
var addEnter = true, noRepeat = true, jobArr = [], phoneArr = [], tdStr = '', trIndex, hasNullMes = false,
    tarInp = $('#exampleTableEvents input'), tarSel = $('#exampleTableEvents select');
var methods = {
    addHandle: function (the_index) {
        hasNullMes = false;
        methods.checkMustMes();
        if (hasNullMes) {
            return;
        }
        if (addEnter) {
            methods.checkRepeat();
            if (noRepeat) {
                methods.setStr();
                $('#exampleTableEvents').append('<tr>' + tdStr + '</tr>');
                $('.xzdd').modal('hide');
            }
        } else {
            methods.setStr();
            $('#exampleTableEvents tr').eq(trIndex).empty().append(tdStr);
            $('.xzdd').modal('hide');
        }
    }, editHandle: function (the_index) {
        var tar = $('#exampleTableEvents tr').eq(the_index);
        var nowConArr = [];
        for (var i = 0; i < tar.find('td').length - 1; i++) {
            var a = tar.children('td').eq(i).html();
            nowConArr.push(a);
        }
        $('.xzdd').modal('show');
        for (var j = 0; j < tarInp.length; j++) {
            tarInp.eq(j).val(nowConArr[j])
        }
        for (var p = 0; p < tarSel.length; p++) {
            var the_p = p + tarInp.length;
            tarSel.eq(p).val(nowConArr[the_p]);
        }
    }, setStr: function () {
        tdStr = '';
        for (var a = 0; a < tarInp.length; a++) {
            tdStr += '<td>' + tarInp.eq(a).val() + '</td>'
        }
        for (var b = 0; b < tarSel.length; b++) {
            tdStr += '<td>' + tarSel.eq(b).val() + '</td>'
        }
        tdStr += '<td><button id = "TableEditor" type="button" class = "btn btn-info btngroup">编辑</button> <button id = "TableDelete" type="button" class = "btn btn-danger btngroup">删除</button></td>';
    }, seachName: function () {
        var a = $('#exampleTableEvents tr');
        var nameVal = $('#exampleTableEvents').val().trim();
        var nameStr = '', nameArr = [];
        if (nameVal === '') {
            bootbox.alert({title: "提示", message: "搜索内容不能为空", closeButton: false})
            return;
        }
        for (var c = 0; c < a.length; c++) {
            var txt = $('td:first', a.eq(c)).html().trim();
            nameArr.push(txt);
        }
        a.hide();
        for (var i = 0; i < nameArr.length; i++) {
            if (nameArr[i].indexOf(nameVal) > -1) {
                a.eq(i).show();
            }
        }
    }, resectList: function () {
        $('#exampleTableEvents tr').show();
    }, checkMustMes: function () {
        if ($('.userName').val().trim() === '') {
            bootbox.alert({title: "提示", message: "姓名为必选项，请填写", closeButton: false})
            hasNullMes = true;
            return
        }
        if ($('.jobNum').val().trim() === '') {
            bootbox.alert({title: "提示", message: "工号为必选项，请填写", closeButton: false})
            hasNullMes = true;
            return
        }
        if ($('.phoneNum').val().trim() === '') {
            bootbox.alert({title: "提示", message: "手机号为必选项，请填写", closeButton: false})
            hasNullMes = true;
            return
        }
    }, checkRepeat: function () {
        jobArr = [], phoneArr = [];
        for (var i = 0; i < $('#exampleTableEvents tr:not(".has_case")').length; i++) {
            var par = '#exampleTableEvents tr:not(".has_case"):eq(' + i + ')';
            var a = $('td:eq(1)', par).html().trim(), b = $('td:eq(2)', par).html().trim();
            jobArr.push(a);
            phoneArr.push(b);
        }
        var jobNum = $('.jobNum').val().trim(), phoneNum = $('.phoneNum').val().trim();
        if (jobArr.indexOf(jobNum) > -1) {
            noRepeat = false;
            bootbox.alert({title: "提示", message: "工号重复了，请重新输入", closeButton: false})
            return;
        }
        if (phoneArr.indexOf(phoneNum) > -1) {
            noRepeat = false;
            bootbox.alert({title: "提示", message: "手机号码重复了，请重新输入", closeButton: false})
            return;
        }
        noRepeat = true;
    }
}